package com.wcarmon.life.vo {

 	import com.adobe.cairngorm.vo.IValueObject;
 	import mx.utils.ObjectUtil;
 	import com.wcarmon.utils.*;
 	
 
	public class BlockVO implements IValueObject {
		include "../../includes/loggerSnippet.as";
		public static var sLog : ILogger = Log.getLogger("com.wcarmon.life.vo.BlockVO");

		public static const STATE_COLLECT : uint = 1;
		public static const STATE_REFERENCE : uint = 2;
		public static const STATE_CALENDAR : uint = 5;
		public static const STATE_NEXT_ACTION : uint = 6;
		public static const STATE_WF : uint = 7;
		public static const STATE_PROJECT_INDEX : uint = 8;
		public static const STATE_UNCOMMITED_NEAR_FUTURE : uint = 4;	//formerly Someday/Maybe
		public static const STATE_UNCOMMITED_DISTANT_FUTURE : uint = 13;


		/*
		public function toString() : String {
			var s : String = "BlockVO[id=" + id;
			s += ", content="+content;
			s += ", deadline="+deadline;
			s += ", state_id="+state_id;
			s += ", isComplete="+isComplete;
			s += ", isRejected="+isRejected;
			s += "]";
			return s;
		} 
		*/
		[Bindable]
		public var id : uint;
		
		[Bindable]
		public var content : String = '';
		
		[Bindable]
		public var outcome : String = '';
		
		[Bindable]
		public var importance : String = 'Low';
		
		//[Bindable]
		//public var urgency : String = 'Low';
		
		[Bindable]
		public var timeInHours : uint = 0;
		
		[Bindable]
		public var deadline : String = '0000-00-00';
		
		[Bindable]
		public var cost : uint = 0;
		
		//[Bindable]
		//public var topic_id : uint = 0;
		
		[Bindable]
		public var state_id : uint = 0;	
		
		[Bindable]
		public var contextIds : String = '';
		
		[Bindable]
		public var projectIds : String = '';
		
		[Bindable]
		public var viewLevel_id : uint = 0;
		
		[Bindable]
		public var delegateTo : String = '';

		[Bindable]
		public var canDelegate : uint = 0;
		
		[Bindable]
		public var dependsOn : String = '';
		
		[Bindable]
		public var isPrivate : uint = 0;
		
		[Bindable]
		public var isComplete : uint = 0;
		
		[Bindable]
		public var isRejected : uint = 0;
		
		[Bindable]
		public var ref_meta : String = '';
		
		[Bindable]
		public var ref_language : String = '';
		
		[Bindable]
		public var ref_code : String = '';
		
		[Bindable]
		public var url1 : String = '';
		
		[Bindable]
		public var url2 : String = '';
		
		[Bindable]
		public var fileIds : String = '';
		
		[Bindable]
		public var somedayLabel : String = '';	//Givon: Rename this universally...
		
		[Bindable]
		public var created : String = '';
		
		[Bindable]
		public var modified : String = '';
		
		[Bindable]
		public var points : uint = 1;


		//--- Fields not in the table ---
		[Bindable]
		public var parents : Array = new Array();
		
		[Bindable]
		public var children : Array = new Array();
		
		[Bindable]
		public var childrenByState : Object;
		
		[Bindable]
		public var isIdle : Boolean;	//calculated on server
		
		[Bindable]
		public var sortRank : uint;
		
		[Bindable]
		public var dependencies : Array;	




		//////////////////////////////////
		//////////////////////////////////
		//////////////////////////////////
		//////////////////////////////////
		//////////////////////////////////

		public function BlockVO(){
		}
		
		public function getStatus() : String {
			if( this.isRejected ){ return 'Rejected'; }
			if( this.isComplete ){ return 'Complete'; }
			return 'Open';
		}
		
		public function deadlineIsEmpty() : Boolean{
			return Utils.isEmptyDate( this.deadline );
		}
		
		public static function makeBlockFromObject( o : Object ) : BlockVO{
			sLog.debug('Creating a blockVO from a server hashmap');
			var b : BlockVO = new BlockVO();
			b.id = o.id;
			b.content = o.content;
			
			//sLog.debug( 'Passed from Server: ' + ObjectUtil.toString(o) );
			
			var props : Array = ['deadline','contextIds','cost','created','canDelegate','deadline',
								'delegateTo','dependsOn','fileIds','importance','isComplete',
								'isPrivate','isRejected','modified','outcome','points','projectIds',
								'ref_code','ref_language','ref_meta','state_id','timeInHours',
								'url1','url2','viewLevel_id',
								'children','parents','sortRank', 
								'childrenByState', 'isIdle'
								/*'emptyDeadlineTime','emptyDeadlineDate','emptyDeadline'*/
								/*'somedayLabel','subState_id','topic_id','urgency',*/];
			
			for( var i : uint =0; i < props.length; i++ ){
				if( o.hasOwnProperty( props[i] ) ){
					//sLog.debug('[makeBlockFromObject] setting property "'+props[i]+'" to value="'+ o[ props[i] ] +'"');
					b[ props[i] ] = o[ props[i] ];
				}else{
					sLog.warn('Trouble translating server hashmap to BlockVO: cannot find property: ' + props[i] );
				}
			}
			return b;		
		}
	}
}